package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.view.AnimationTabHost;
import com.example.permission.myjbm.siweb.PopMenu;
import com.example.permission.myjbm.siweb.PopMenuItem;
import com.example.permission.myjbm.siweb.PopMenuItemListener;
import com.example.permission.myjbm.utils.ReleasePopup;
import com.example.zp.liaotian.TestDuiHuaActivity;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 李征
 * Created by 家帮忙 on 2017/1/9.
 */

public class MainActivity extends TabActivity implements RadioGroup.OnCheckedChangeListener {
    @InjectView(android.R.id.tabcontent)
    FrameLayout tabcontent;
    @InjectView(android.R.id.tabs)
    TabWidget tabs;

    static RadioButton radioButton1;

    static RadioButton radioButton2;

    static TextView radioButton3;

    static RadioButton radioButton4;

    static RadioButton radioButton5;

    RadioGroup group;

    static AnimationTabHost tabhost;
    private SharedPreferences mSharedPreferences;
    private PopMenu mPopMenu;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        ButterKnife.inject(this);
//        ExitAppUtlis.getInstance().addActivity(this);
        mSharedPreferences = getSharedPreferences("sinopecall",
                Activity.MODE_PRIVATE);
        setUpView();
        tabhost.setOpenAnimation(true);
        setGainTab();
    }

    public void setGainTab() {
        //获取TabHost组件
        TabHost tabhosts = getTabHost();
        //创建一个标签
        TabHost.TabSpec tabspec = tabhosts.newTabSpec("NEWHOME").setIndicator("NEWHOME");
        //需要跳转的页面
        tabspec.setContent(new Intent(MainActivity.this, NewHomeActivity.class));
        //把tabhost添加到标签中
        tabhost.addTab(tabspec);
        TabHost.TabSpec tabspec1 = tabhosts.newTabSpec("MESSAGEHOME").setIndicator("MESSAGEHOME");
        tabspec1.setContent(new Intent(MainActivity.this, MessAgeHomeActivity.class));
        tabhost.addTab(tabspec1);
//        TabHost.TabSpec tabspec2 = tabhosts.newTabSpec("ISSOEHOME").setIndicator("ISSOEHOME");
//        tabspec2.setContent(new Intent(MainActivity.this, IssoHomeActivity.class));
//        tabhost.addTab(tabspec2);
        TabHost.TabSpec tabspec3 = tabhosts.newTabSpec("ADDRESSBACKHOME").setIndicator("ADDRESSBACKHOME");
        tabspec3.setContent(new Intent(MainActivity.this, TestDuiHuaActivity.class));
        tabhost.addTab(tabspec3);
        TabHost.TabSpec tabspec4 = tabhosts.newTabSpec("CARDPACKAGE").setIndicator("CARDPACKAGE");
        tabspec4.setContent(new Intent(MainActivity.this, CardHomeActivity.class));
        tabhost.addTab(tabspec4);
        group.setOnCheckedChangeListener(this);
    initFabu();
        radioButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if (!mPopMenu.isShowing()) {
//                    mPopMenu.show();
//                }

                ReleasePopup.getInstance(MainActivity.this).show(v);
            }
        });

    }

    public static void goNewHome() {
        tabhost.setCurrentTabByTag("NEWHOME");
        radioButton1.setChecked(true);
    }

    public static void goMessHome() {
        tabhost.setCurrentTabByTag("MESSAGEHOME");
        radioButton2.setChecked(true);
    }

//    public static void goIssoHome() {
//        tabhost.setCurrentTabByTag("ISSOEHOME");
//        radioButton3.setChecked(true);
//    }

    public static void goAddresHome() {
        tabhost.setCurrentTabByTag("ADDRESSBACKHOME");
        radioButton4.setChecked(true);
    }

    public static void goCardHome() {
        tabhost.setCurrentTabByTag("CARDPACKAGE");
        radioButton5.setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.radioButton1:
                tabhost.setCurrentTabByTag("NEWHOME");

                break;
            case R.id.radioButton2:
                tabhost.setCurrentTabByTag("MESSAGEHOME");

                break;
           // case R.id.radioButton3:
//                tabhost.setCurrentTabByTag("ISSOEHOME");
              //  Log.e("main","执行了发布页面");
           //     break;
            case R.id.radioButton4:
                tabhost.setCurrentTabByTag("ADDRESSBACKHOME");

                break;
            case R.id.radioButton5:
                if (mSharedPreferences.getBoolean("IsLogin",false)!=true){
                    startActivity(new Intent(this, LoginActivity.class));
                    radioButton1.setChecked(true);
                }else {
                    tabhost.setCurrentTabByTag("CARDPACKAGE");
                }
                break;
        }

    }
    public void setUpView() {
        group = (RadioGroup) findViewById(R.id.group);
        radioButton1 = (RadioButton) findViewById(R.id.radioButton1);
        radioButton2 = (RadioButton) findViewById(R.id.radioButton2);
        radioButton3 = (TextView) findViewById(R.id.radioButton3);
        radioButton4 = (RadioButton) findViewById(R.id.radioButton4);
        tabhost = (AnimationTabHost) findViewById(android.R.id.tabhost);

    }
    //发布
    public void initFabu(){
        mPopMenu = new PopMenu.Builder().attachToActivity(MainActivity.this)
                .addMenuItem(new PopMenuItem("", getResources().getDrawable(R.mipmap.guanggao)))
                .addMenuItem(new PopMenuItem("", getResources().getDrawable(R.mipmap.gonggong)))
                .addMenuItem(new PopMenuItem("", getResources().getDrawable(R.mipmap.huzhu)))
                .addMenuItem(new PopMenuItem("", getResources().getDrawable(R.mipmap.quwei)))
                .setOnItemClickListener(new PopMenuItemListener() {
                    @Override
                    public void onItemClick(PopMenu popMenu, int position) {
                      //  Toast.makeText(MainActivity.this, "浣犵偣鍑讳簡绗? + position + "涓綅缃?, Toast.LENGTH_SHORT).show();
                    }
                })
                .build();

//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//            }
//        });



}

}
